package com.wisdge.utils.security;

import com.huasheng.passwordencrypt.DecryptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AESForNodejs {
    private static final Logger logger = LoggerFactory.getLogger(AESForNodejs.class);

    private static byte[] keys = new byte[]{-2, -1, -23, -110, -122, 101, 115, 28, 109, 106, -113, -108, 103, 48, -125, 8};

    private static byte[] iv = new byte[]{-103, -86, 62, 104, -19, -127, 115, -96, -18, -48, 102, -124, -18, -48, 102, -124};

    private static byte[] aad = new byte[]{77, 35, -61, -50, -61, 52, -76, -101, -37, 55, 12, 67, Byte.MAX_VALUE, -20, 120, -34};

    private static byte[] cipher;

    private static byte[] plain;

    public static void main(String[] args) {
        String mobile = "18675571253abc";
        String encc = encrypt(mobile, "");
        System.out.println(encc);
        String dncc = decrypt(encc, "");
        System.out.println(dncc);
    }

    public static String decrypt(String encrypted, String key) {
        if (encrypted == null || encrypted.length() == 0)
            return "";
        return decrypt("sm4", "cbc", encrypted, key);
    }

    public static String encrypt(String encrypted, String key) {
        if (encrypted == null || encrypted.length() == 0)
            return "";
        return encrypt("sm4", "cbc", encrypted, key);
    }

    public static String encrypt(String ciphername, String funciton, String input, String key) {
        String message_enc = DecryptUtil.encrypt(input);
        logger.debug("huasheng DecryptUtil.encrypt...");
        return message_enc;
    }

    public static String decrypt(String ciphername, String funciton, String encrypted, String key) {

        String message_dec = DecryptUtil.decrypt(encrypted);
        logger.debug("huasheng DecryptUtil.decrypt...");
        return message_dec;
    }

    public static byte[] toByteArray(String hexString) {
        if (hexString == null || hexString.length() <= 0)
            throw new IllegalArgumentException("this hexString must not be empty");
        hexString = hexString.toLowerCase();
        byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xFF);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xFF);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

    public static String toHexString(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1)
            throw new IllegalArgumentException("this byteArray must not be null or empty");
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if ((byteArray[i] & 0xFF) < 16)
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();
    }
}
